package com.yonyou.cyxdms.sysAuth.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.sysAuth.service.constants.CommonConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.AreaManagerDto;
import com.yonyou.cyxdms.sysAuth.service.dto.user.EmpDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.user.EmployeeDTO;
import com.yonyou.cyxdms.sysAuth.service.service.EmpService;
import com.yonyou.cyxdms.sysAuth.service.vo.employee.EmployeeAddVO;
import com.yonyou.cyxdms.sysAuth.service.vo.user.RoleUserVo;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.DealerOrgInfoS;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author zhangxianchao
 * @since 2018-09-13
 */
@RestController
@RequestMapping("/emp")
public class EmpController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService iempposervice;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @ResponseBody
    @GetMapping
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition, @RequestParam
            ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        return iempposervice.selectPageBysql(page, queryCondition);
    }

    /**
     * 进行数据修改
     *
     * @param id 数据主键ID
     * @return Map<String   ,   Object> 数据对应的map 结构
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @ResponseBody
    @GetMapping(value = "/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        return iempposervice.getById(id);
    }

    /**
     * 进行数据新增
     *
     * @param empDTO 需要保存的DTO
     * @return Map<String , Object> 返回的数据map
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @ResponseBody
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> insert(@RequestBody EmpDTO empDTO) {
        logger.debug("empDTO:{}", empDTO);
        return iempposervice.insert(empDTO);
    }

    /**
     * 进行数据修改
     *
     * @param id     需要修改数据的ID
     * @param empDTO 需要保存的DTO
     * @return Map<String   ,   Object> 返回的数据map
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Long id, @RequestBody EmpDTO empDTO) {
        logger.debug("empDTO:{}", empDTO);
        return iempposervice.update(id, empDTO);
    }

    /**
     * 根据id删除对象
     *
     * @param id 需要修改数据的ID
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteById(@PathVariable("id") Long id) {
        logger.debug("ID:{}", id);
        iempposervice.deleteById(id);
    }

    /**
     * 根据IDs批量删除对象
     *
     * @param ids 需要修改数据的ID集合
     * @author zhangxianchao
     * @since 2018-09-13
     */
    @DeleteMapping(value = "/batch/{ids}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteByIds(@PathVariable("ids") String ids) {
        iempposervice.deleteBatchIds(ids);
    }

    /**
     * 来自dms人员信息新增
     *
     * @param employeeDTO
     * @return java.lang.Long
     * @author zhangsx
     * @since 2018/10/29
     */
    @PostMapping(value = "/addEmployee/fromdms")
    public Long addEmployeeFromDms(@RequestBody EmployeeDTO employeeDTO) {
        return iempposervice.addEmployeeFromDms(employeeDTO);
    }

    /**
     * 人员信息更新
     *
     * @param employeeDTO
     * @return void
     * @author zhangsx
     * @since 2018/10/29
     */
    @PostMapping(value = "/updateEmployee/fromdms")
    public boolean updateEmployeeFromDms(@RequestBody EmployeeDTO employeeDTO) {
        iempposervice.updateEmployeeFromDms(employeeDTO);
        return true;
    }


    /**
     * 人员信息更新
     *
     * @param employeeDTO
     * @return void
     * @author zhangsx
     * @since 2018/10/29
     */
    @PostMapping(value = "/updateEmployee/wuling/fromdms")
    public boolean updateWlEmployeeFromDms(@RequestBody EmployeeDTO employeeDTO) {
        iempposervice.updateWlEmployeeFromDms(employeeDTO);
        return true;
    }

    /**
     * 通过手机号获取顾问id
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/15
     */
    @GetMapping("/phone")
    public Integer getEmployeeByPhone(@RequestParam("phone") String phone,
                                      @RequestParam("appId") String appId) {
        logger.info("通过手机号获取顾问id");
        return iempposervice.getEmployeeByPhone(phone, appId);
    }

    /**
     * 通过姓名获取顾问id
     */
    @GetMapping("/employeeName")
    public List<Integer> getEmployeeByEmployeeName(@RequestParam("employeeName") String employeeName
    ) {
        return iempposervice.getEmployeeByEmployeeName(employeeName);
    }

    /**
     * 根据userIds获取姓名和手机
     *
     * @param userIds      需要查询的id
     * @param consultantId 此id必须要查询
     * @param isContain    true,正向查询ids,false反向查询
     * @return: Map<userID,Map<String,String>>
     * @auther: tanjie
     * @since: 2019/9/21
     */
    @RequestMapping(value = "/getEmpInfoByUserId", method = RequestMethod.POST)
    public Map<String, Map<String, String>> getEmpInfoByUserId(@RequestBody List<String> userIds,
                                                               @RequestParam("isContain") Boolean isContain,
                                                               @RequestParam(value = "consultantId", required = false) String consultantId,
                                                               @RequestParam("appId") String appId,
                                                               @RequestParam("ownerCode") String ownerCode) {

        logger.info("批量获取顾问信息开始----");
        return this.iempposervice.getEmpInfoByUserId(userIds, isContain, consultantId, appId, ownerCode);
    }

    /**
     * 通过手机号查询顾问的orgId.companyCode,ownerCode,
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/21
     */
    @PostMapping("/getOrginfoByPhone")
    public RoleUserVo getOrginfoByPhone(@RequestBody RoleUserVo roleUserVo) {
        return this.iempposervice.getOrginfoByPhone(roleUserVo, CommonConstants.DCRC_COMMISSIONER);
    }

    @PostMapping("/getOrgInfoByPhoneAndOwnerCode")
    public RoleUserVo getOrgInfoByPhoneAndOwnerCode(@RequestBody RoleUserVo roleUserVo) {
        return this.iempposervice.getOrgInfoByPhoneAndOwnerCode(roleUserVo, CommonConstants.DCRC_COMMISSIONER);
    }


    /**
     * 顾问校验接口
     */
    @PostMapping("/consultantVerification")
    public List<Map<String, String>> consultantVerification(@RequestBody List<EmployeeDTO> consultantList) {
        return this.iempposervice.consultantVerification(consultantList);
    }

    /**
     * 通过userid查emp信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/22
     */
    @GetMapping("/queryEmpInfoByuserId")
    @ApiOperation("通过userId查询对应顾问信息")
    public EmpDTO queryEmpInfoByuserId(@RequestParam("userId") Long userId) {
        return this.iempposervice.queryEmpInfoByUserId(userId);
    }

    /**
     * 新增员工时校验员工身份证号
     *
     * @auther: 李贺
     * @since: 2019/11/12
     */
    @GetMapping("/checkCertificateId")
    @ApiOperation("新增员工时校验员工身份证号")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public EmployeeAddVO checkCertificateId(@ApiParam(name = "certificateID", value = "certificateID") @RequestParam("certificateID") String certificateId) {
        return iempposervice.checkCertificateId(certificateId);
    }


    /**
     * USERID 批量获取EMP信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/22
     */
    @GetMapping("/queryEmpInfoByUserIds")
    @ApiOperation("批量获取顾问 信息")
    public Map<Long, EmpDTO> queryEmpInfoByUserIds(@RequestParam("ids") List<Long> ids) {
        return this.iempposervice.queryEmpInfoByUserIds(ids);
    }

    /**
     * USERID 批量获取EMP信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/22
     */
    @PostMapping("/queryEmpInfoByUserIdsPost")
    @ApiOperation("批量获取顾问 信息")
    public Map<Long, EmpDTO> queryEmpInfoByUserIdsPost(@RequestBody List<Long> ids) {
        return this.iempposervice.queryEmpInfoByUserIds(ids);
    }

    /**
     * USERCODE获取信息
     *
     * @param:
     * @return: map<userCode,empDTO>
     * @auther: tanjie
     * @since: 2019/10/22
     */
    @PostMapping("/queryEmpInfoByUserCodes")
    @ApiOperation("批量获取顾问信息")
    public Map<String, EmpDTO> queryEmpInfoByUserCodes(@RequestBody DealerOrgInfoS dealerOrgAreaVO) {
        return this.iempposervice.queryEmpInfoByUserCodes(dealerOrgAreaVO.getUserCodes(), dealerOrgAreaVO.getAppId());
    }

    /**
     * 通过区域id获取区域经理
     * @param orgId
     * @return
     */
    @GetMapping("/queryAreaManagerByOrgId")
    @ApiOperation("通过区域id获取区域经理")
    public List<EmpDTO> queryAreaManagerByOrgId(@RequestParam(value = "orgId") Long orgId){
        return this.iempposervice.queryAreaManagerByOrgId(orgId);
    }

    @GetMapping("/queryAreaManagerAll")
    @ApiOperation("获取所有区域经理")
    public List<AreaManagerDto> queryAreaManagerAll(){
        return this.iempposervice.queryAreaManagerAll();
    }

}